package com.sike.netty.im.server.util;

import io.netty.channel.Channel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

/**
 * 登录util
 *
 * @author chenssy
 * @date 2023/09/17
 */
public class LoginUtil {

    /**
     * 登录状态
     */
    private final static AttributeKey<Boolean> LOGIN = AttributeKey.valueOf("login");

    /**
     * 登录用户
     */
    private final static AttributeKey<String> LONIG_USER = AttributeKey.valueOf("login_user");


    /**
     * 标记为登录
     *
     * @param channel Channel
     */
    public static void markAsLogin(Channel channel) {
        channel.attr(LOGIN).set(true);
    }


    /**
     * Channel 是否已登录
     *
     * @param channel Channel
     * @return
     */
    public static Boolean hasLogin(Channel channel) {
        Attribute<Boolean> login = channel.attr(LOGIN);
        return login.get() != null;
    }

    /**
     * 保存登录用户
     *
     * @param channel  Channel
     * @param userId   用户Id
     */
    public static void saveLoginUser(Channel  channel,String userId) {
        channel.attr(LONIG_USER).set(userId);
    }


    /**
     * 获取登录用户
     *
     * @param channel Channel
     * @return 登录用户名
     */
    public static String getLoginUser(Channel channel) {
        return channel.attr(LONIG_USER).get();
    }
}
